//
//  UIScrollView+EmptyView.swift
//  YuanJuBian
//
//  Created by apple on 2022/2/24.
//

import Foundation
import UIKit

#if canImport(EmptyDataSet_Swift) && canImport(SnapKit)
import EmptyDataSet_Swift
import SnapKit

extension UIScrollView{
    
    func setCustomEmptyView(_ customView:UIView ,vericalOffset:CGFloat = 20){
        
        self.customEmptyView = customView
        self.vericalOffset = vericalOffset
        
        self.emptyDataSetSource = self
        self.emptyDataSetDelegate = self
    }
}
extension UIScrollView : EmptyDataSetDelegate{
    
    public func emptyDataSetShouldAllowTouch(_ scrollView: UIScrollView) -> Bool {
        
        return true
    }
    public func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView) -> Bool {
        
        return true
    }
}
extension UIScrollView : EmptyDataSetSource{
    
    public func customView(forEmptyDataSet scrollView: UIScrollView) -> UIView? {
        
        return self.customEmptyView
    }
    public func verticalOffset(forEmptyDataSet scrollView: UIScrollView) -> CGFloat {

        return self.vericalOffset
    }
}

private var CustomEmptyViewKey : String = "keyForCustomEmptyView"
private var verticalOffsetKey : String = "keyForVerticalOffset"

extension UIScrollView{
    
    private var vericalOffset : CGFloat{
        
        set{
            objc_setAssociatedObject(self, &verticalOffsetKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get{
            if let value = objc_getAssociatedObject(self, &verticalOffsetKey) as? CGFloat{
                return value
            }
            return 0.0
        }
    }
    
    private var customEmptyView : UIView?{
        
        set{
            objc_setAssociatedObject(self, &CustomEmptyViewKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
        get{
            guard let value = objc_getAssociatedObject(self, &CustomEmptyViewKey) as? UIView else {
                return nil
            }
            return value
        }
    }
}
#endif
